package com.jsmy.softapp.service;

import android.Manifest;
import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.ActivityCompat;

import com.jsmy.softapp.MyApp;
import com.jsmy.softapp.util.MyLog;
import com.jsmy.softapp.util.ToastUtil;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 * <p>
 * helper methods.
 */
public class LocationService extends IntentService implements LocationListener {

    private static final String TAG = "LocationService";
    private static final String TAG1 = "LocationService1";
    private static final String TAG2 = "LocationService2";
    private static final String SERVICE_NAME = "LocationService";

    private static final long MIN_TIME = 1000l;
    private static final float MIN_DISTANCE = 10f;

    private LocationManager locationManager;
    private Location location;

    public LocationService() {
        super(SERVICE_NAME);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        MyLog.showLog(TAG, "onHandleIntent");
        if (locationManager == null) {
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        }

            /*
             * 进行定位
			 * provider:用于定位的locationProvider字符串
			 * minTime:时间更新间隔，单位：ms
			 * minDistance:位置刷新距离，单位：m
			 * listener:用于定位更新的监听者locationListener
			 */
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        } else {
            if (locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null || locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
                MyLog.showLog(TAG, "正在定位");
                try {
                    List<String> providerList = locationManager.getProviders(true);
                    if (providerList.contains(LocationManager.GPS_PROVIDER)) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    } else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    }
                    MyApp.getMyApp().longitude = ""+location.getLongitude();
                    MyApp.getMyApp().latitude = "" + location.getLatitude();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtil.showShort(getApplicationContext(), "无法定位，请打开定位服务!");
                    }
                });
            }
        }


    }

    @Override
    public void onLocationChanged(Location location) {
        //得到纬度
        double latitude = location.getLatitude();
        MyLog.showLog(TAG, "C 纬度 =" + latitude);
        //得到经度
        double longitude = location.getLongitude();
        MyLog.showLog(TAG, "C 经度 =" + longitude);
        MyApp.getMyApp().longitude = ""+location.getLongitude();
        MyApp.getMyApp().latitude = "" + location.getLatitude();

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

}
